package com.comdosoft.comdoandroidbase;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;

import com.comdosoft.comdoandroidbase.activity.DataPickActivity;
import com.comdosoft.comdoandroidbase.activity.DateTimePickerActivity;
import com.comdosoft.comdoandroidbase.activity.MapActivity;
import com.comdosoft.comdoandroidbase.activity.MipcaCaptureActivity;
import com.comdosoft.comdoandroidbase.activity.PullToRefreshListViewActivity;
import com.comdosoft.comdoandroidbase.gallery.ImagePagerActivity;
import com.comdosoft.comdoandroidbase.leftmenu.LeftMenuFragment;
import com.comdosoft.comdoandroidbase.util.DataUtil;
import com.comdosoft.comdoandroidbase.util.config;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity implements LeftMenuFragment.OnLeftMenuItemClickListener {

    private Toolbar toolbar;
    private DrawerLayout dlhome;
    private ActionBarDrawerToggle drawerToggle;
    private FragmentManager fm;
    public static int SCREENWIDTH = 480;
    public static int SCREENHEIGH = 360;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        SCREENWIDTH = dm.widthPixels;
        SCREENHEIGH = dm.heightPixels;
        config.initImageLoader(this);
        initView();
    }

    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // 给Toolbar左上角加一个返回按钮
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        dlhome = (DrawerLayout) findViewById(R.id.dl_home);
        drawerToggle = new ActionBarDrawerToggle(this, dlhome, toolbar, 0, 0);
        // 该方法会自动和actionBar关联, 将开关的图片显示在了action上，如果不设置，也可以有抽屉的效果，不过是默认的图标
        drawerToggle.syncState();
        dlhome.setDrawerListener(drawerToggle);
        fm = getSupportFragmentManager();
        fm.beginTransaction().add(R.id.ll_menu, new LeftMenuFragment()).commit();
    }

    @Override
    public void onItemClick(Object itemData) {
        dlhome.closeDrawers();
        HashMap<String, Object> item = (HashMap<String, Object>) itemData;
        if(item.get(DataUtil.MENU_KEY_ITEM_NAME).toString().equals("百度地图附近列表")){
            startActivity(new Intent(MainActivity.this,MapActivity.class));
        }else if(item.get(DataUtil.MENU_KEY_ITEM_NAME).toString().equals("相册")){
            Intent intent = new Intent(this, ImagePagerActivity.class);
            intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, config.urls);
            (this).startActivityForResult(intent, 0);
        }else if(item.get(DataUtil.MENU_KEY_ITEM_NAME).toString().equals("扫描二维码")){
            startActivity(new Intent(MainActivity.this, MipcaCaptureActivity.class));
        }else if (item.get(DataUtil.MENU_KEY_ITEM_NAME).toString().equals("上下拉刷新")){
            startActivity(new Intent(MainActivity.this, PullToRefreshListViewActivity.class));
        }else if (item.get(DataUtil.MENU_KEY_ITEM_NAME).toString().equals("日历备注")){
            startActivity(new Intent(MainActivity.this, com.comdosoft.comdoandroidbase.ui.activity.MainActivity.class));
        }else if (item.get(DataUtil.MENU_KEY_ITEM_NAME).toString().equals("日期选择")){
            startActivity(new Intent(MainActivity.this, DataPickActivity.class));
        }else if (item.get(DataUtil.MENU_KEY_ITEM_NAME).toString().equals("日期时间选择")){
            startActivity(new Intent(MainActivity.this, DateTimePickerActivity.class));
        }else{
            Fragment fragment = (Fragment) item.get(DataUtil.MENU_KEY_FRAGMENT);
            setTitle(item.get(DataUtil.MENU_KEY_ITEM_NAME).toString());
            fm.beginTransaction().replace(R.id.fl_main, fragment).commit();
        }

    }

}
